<template>
  <div class="app-container">
    <div>
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label="内容：" prop="content">
          <el-input v-model="searchForm.content" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="fetchData(true)">查询</el-button>
          <el-button type="danger" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="id" width="95" prop="id" />
      <el-table-column label="内容" width="150" prop="content" />
      <el-table-column label="回复内容" width="150" prop="reply" />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="操作" prop="op">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEdit(scope.row)"
          >回复</el-button>
          <el-button
            type="danger"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :page-sizes="paginationConfig.pageSizes"
      :layout="paginationConfig.layout"
      :total="paginationConfig.total"
      :page="paginationConfig.page"
      :limit="paginationConfig.pageSize"
      @pagination="handleChangePage"
    />
  </div>
</template>

<script>
import { deleteHouses, getHousesList } from '@/api/house'
import {
  deleteMessageBoard,
  getMessageBoardList,
  replyMessageBoardDetail,
  updateMessageBoardDetail
} from '@/api/message_board'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        content: '',
        state: ''
      },
      options: [
        {
          label: '全部',
          value: ''
        },
        {
          label: '上架',
          value: 1
        },
        {
          label: '下架',
          value: 0
        }
      ],
      paginationConfig: {
        direction: 'center', // 分页位置
        layout: 'total, sizes, prev, pager, next, jumper', // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10 // 每页条数-默认10
      },
      curryNew: {},
      dialogNewsDetailVisible: false
    }
  },
  created() {
    this.fetchData(true)
  },
  methods: {
    fetchData(isClear = false) {
      if (isClear) {
        this.paginationConfig.page = 1
      }
      this.listLoading = true
      const { page, pageSize } = this.paginationConfig
      const obj = {
        page,
        pageSize
      }
      getMessageBoardList({
        ...obj,
        ...this.searchForm
      }).then((response) => {
        if (response.code === 200) {
          this.list = response.data.list
          this.paginationConfig.total = response.data.total
          this.listLoading = false
        }
      })
    },
    handleChangePage({ page, pageSize }) {
      this.paginationConfig.page = page
      this.paginationConfig.pageSize = pageSize
      this.fetchData()
    },

    onReset() {
      this.$refs.searchFormRef.resetFields()
      this.fetchData()
    },
    handleEdit(e) {
      this.$prompt('请输入回复内容', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(({ value }) => {
          replyMessageBoardDetail({
            id: e.id,
            reply: value
          }).then((response) => {
            if (response.code === 200) {
              this.$message.success(response.data)
              this.fetchData()
            }
          })
        })
        .catch(() => {

        })
    },
    async handleDel(e) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteMessageBoard({
          id: e.id
        })
        if (res.code === 200) {
          this.$message.success(res.data)
          this.fetchData()
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-content {
  width: 375px;
  height: 600px;
  overflow: hidden;
  overflow-y: scroll;
}
</style>
